package top.fengyulou.xiaofeng.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import top.fengyulou.xiaofeng.common.UuidUtil;
import top.fengyulou.xiaofeng.domain.Label;
import top.fengyulou.xiaofeng.dto.LabelDto;
import top.fengyulou.xiaofeng.mapper.LabelMapper;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName LabelService
 * @Description TODO
 * @Author qwer
 * @Date 2021/8/22 1:54
 * @Version 1.0
 */
@Service
public class LabelService {
    
    @Resource
    private LabelMapper labelMapper;

    /**
     * 列表查询
     */
    public void listPage(LabelDto query) {
        //查询数据
        PageHelper.startPage(query.getPageNumber(), query.getPageSize());
        List<LabelDto> labelList = labelMapper.list(query);
        PageInfo<LabelDto> pageInfo = new PageInfo<>(labelList);

        //返回结果
        query.setTotal(pageInfo.getTotal());
        query.setRows(labelList);
    }

    //列表
    public List<LabelDto> list(LabelDto query){
        return labelMapper.list(query);
    }

    //列表
    public List<LabelDto> listTree(LabelDto query){
        return labelMapper.listTree(query);
    }

    /**
     * id查询
     */
    public LabelDto getById(String id) {
        LabelDto labelDto = labelMapper.getById(id);
        return labelDto;
    }

    //保存
    public void save(Label label) {
        if (StringUtils.isNotBlank(label.getId())) {
            label.setUpdateTime(new Date());
            label.setLastUpdateUserId(label.getCurrentUserId());
            labelMapper.update(label);
        } else {
            label.setId(UuidUtil.getShortUuid());
            label.setCreateTime(new Date());
            label.setCreateUserId(label.getCurrentUserId());
            labelMapper.insert(label);
        }
    }

    //删除
    public void delete(String[] ids) {
        labelMapper.delete(ids);
    }
}

